package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.model.Dot;
import com.stylefeng.guns.common.persistence.model.Region;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.business.service.IDotService;
import com.stylefeng.guns.modular.system.service.IRegionService;
import com.stylefeng.guns.modular.system.warpper.DeliRanWarpper;
import com.stylefeng.guns.modular.system.warpper.DotWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.DeliveryRange;
import com.stylefeng.guns.modular.system.service.IDeliveryRangeService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配送范围控制器
 *
 * @author fengshuonan
 * @Date 2018-01-16 21:36:33
 */
@Controller
@RequestMapping("/deliveryRange")
public class DeliveryRangeController extends BaseController {

    private String PREFIX = "/system/deliveryRange/";

    @Autowired
    private IDeliveryRangeService deliveryRangeService;

    @Resource
    private IRegionService regionService;

    @Resource
    private IDotService dotService;

    /**
     * 跳转到配送范围首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "deliveryRange.html";
    }

    /**
     * 跳转到添加配送范围
     */
    @RequestMapping("/deliveryRange_add")
    public String deliveryRangeAdd( Model model) {
    //获取省份信息
        List<Region> regionList=regionService.selectListByPid("1");//查找中国下级区域，即省份信息
    //获取本企业未添加配送范围的网点
        List <Dot> list=deliveryRangeService.getUndistriDots(ShiroKit.getUser().getDeptId());
        model.addAttribute("dots",list);
        model.addAttribute("regionList",regionList);
        return PREFIX + "deliveryRange_add.html";
    }

    /**
     * 跳转到修改配送范围
     */
    @RequestMapping("/deliveryRange_update/{deliveryRangeId}")
    public String deliveryRangeUpdate(@PathVariable Integer deliveryRangeId, Model model) {
        DeliveryRange deliveryRange = deliveryRangeService.selectById(deliveryRangeId);
        Dot dot =dotService.selectById(deliveryRange.getDotId());
        model.addAttribute("item",deliveryRange);
        model.addAttribute("dot",dot);
        List<Region> pro=regionService.selectListByPid("1");
        model.addAttribute("proList",pro);
        List<Region> city=regionService.selectListByPid(deliveryRange.getProPlaceName()+"");
        model.addAttribute("cityList",city);
        List<Region> reg=regionService.selectListByPid(deliveryRange.getCityPlaceName()+"");
        model.addAttribute("regList",reg);
       // LogObjectHolder.me().set(deliveryRange);
        return PREFIX + "deliveryRange_edit.html";
    }
       /**
     * 获取配送范围列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) Integer deptid,Integer limit, Integer offset) {
        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",deptid==null?ShiroKit.getUser().getDeptId():deptid);
        m.put("name",name);
        List<Map<String, Object>> deliRans = deliveryRangeService.list(page,m);
        pageInfoBT.setData(deliRans);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 新增配送范围
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeliveryRange deliveryRange) {
        deliveryRange.setDeptId(ShiroKit.getUser().getDeptId());
        deliveryRange.setCreateUserId(ShiroKit.getUser().getId());
        if(deliveryRangeService.insert(deliveryRange)){
            return super.SUCCESS_TIP;
        }else{
            return new ErrorTip(Const.FAILUER,"新增失败");
        }
    }

    /**
     * 删除配送范围
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer deliveryRangeId) {
        if( deliveryRangeService.deleteById(deliveryRangeId)){
            return super.SUCCESS_TIP;
        }else{
            return new ErrorTip(Const.FAILUER,"删除失败");
        }
    }

    /**
     * 修改配送范围
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DeliveryRange deliveryRange) {
        deliveryRange.setUpdateTime(new Date()  );
        if(deliveryRangeService.updateById(deliveryRange)){
            return super.SUCCESS_TIP;
        }else{
            return new ErrorTip(Const.FAILUER,"操作失败");
        }
    }

    /**
     * 配送范围详情
     */
    @RequestMapping(value = "/detail/{deliveryRangeId}")
    @ResponseBody
    public Object detail(@PathVariable("deliveryRangeId") Integer deliveryRangeId) {
        return deliveryRangeService.selectById(deliveryRangeId);
    }
}
